package com.openlab.activity;

import com.openlab.action.LoggingAction;
import com.openlab.action.LoggingThread;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class LoggingActivity extends Activity{
	private Bundle webServiceParams;
	private boolean savePwd;
	private SharedPreferences sp;
	private Context context;
	private Handler loggingHandler;
	private String result;
	private LoggingAction loggingAction;
	private Intent loggingIntent;
	private static ImageView image0;
	private static ImageView image1;
	private static ImageView image2;
	private static ImageView image3;
	private static int imageIndex = 0;
	private LoggingThread update;
	private boolean exitActivity = false;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.logging_page); 
		context = getApplicationContext();
        sp = getSharedPreferences("login",Context.MODE_PRIVATE);
        loggingIntent = new Intent();
        image0 = (ImageView)findViewById(R.id.image0);
        image1 = (ImageView)findViewById(R.id.image1);
        image2 = (ImageView)findViewById(R.id.image2);
        image3 = (ImageView)findViewById(R.id.image3);      
		savePwd = getIntent().getBooleanExtra("savePwd", false);
		webServiceParams = getIntent().getBundleExtra("webServiceParams");
		loggingAction = new LoggingAction(sp,context);
		handleResult();	
		update = new LoggingThread(loggingHandler);
		update.start();
		loggingAction.submit(webServiceParams, savePwd, loggingHandler);
	}

	//refresh the logging flash
	public void updateImage(){
		switch(imageIndex){
			case 0:{
				image0.setVisibility(View.VISIBLE);
				break;
			}
			case 1:{
				image1.setVisibility(View.VISIBLE);
				break;
			}
			case 2:{
				image2.setVisibility(View.VISIBLE);
				break;
			}
			case 3:{
				image3.setVisibility(View.VISIBLE);
				break;
			}
			case 4:{
				image0.setVisibility(View.INVISIBLE);
				image1.setVisibility(View.INVISIBLE);
				image2.setVisibility(View.INVISIBLE);
				image3.setVisibility(View.INVISIBLE);
				imageIndex=-1;
				break;
			}
		}	
		imageIndex++;
	}

	
	public void handleResult(){
		loggingHandler = new Handler(){
			public void handleMessage(Message msg) {
				Bundle resultBundle = msg.getData();
				result = resultBundle.getString("result");
				String refresh = resultBundle.getString("refreshImage");
				if(refresh != null && refresh.equals("true")){
					updateImage();
					return;
				}else{
					update.cancle(true);
					if(exitActivity == true){						
						return;
					}
					loggingIntent.putExtra("result", result);
						if(loggingAction.checkResult(result)){   					//success
//					if(true){
						Toast.makeText(LoggingActivity.this, getString(R.string.loginSuccess), Toast.LENGTH_SHORT).show();
						loggingIntent.setClass(LoggingActivity.this, HomeActivity.class); 
					}else{														//fail
						Toast.makeText(LoggingActivity.this, getString(R.string.loginFail), Toast.LENGTH_SHORT).show();
						loggingIntent.setClass(LoggingActivity.this, LoginActivity.class);
					}
					finish(); 
					LoggingActivity.this.startActivity(loggingIntent);
				}
			}	
		};		
	}	
		
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK ) {	 
	    	   exitActivity = true;
	    	   if(update.cancle(exitActivity)){
	    		   imageIndex = 0;
	    		   loggingHandler.removeMessages(0);
	    		   LoggingActivity.this.finish();
	    		   loggingIntent.setClass(LoggingActivity.this, LoginActivity.class);
	    		   LoggingActivity.this.startActivity(loggingIntent);			
	    	   }	    	   
//	    	   super.onBackPressed();	    	   
	    	   return false;
    	   }	  
		return super.onKeyDown(keyCode, event);
	} 
}
